package com.bird.generate.ddd.domain.generateutil

import cn.hutool.core.io.FileUtil
import com.bird.generate.ddd.domain.*

class RemoteGenerate {

    companion object {
        /**
         * 生成remote代码
         */
        fun adapterGenerate(path: String,aggregateRoot: AggregateRootEntity) {
            var remoteRootPath = path.appendNextPath(aggregateRoot.moduleName.lowercase())+"bc".appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound-remote").appendNextPath(JAVA_PATH)
                .appendNextPath("${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/northbound/remote/${aggregateRoot.enName.lowercase()}biz")
            //root 包路径
            var remoteJavaPath =
                "${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/northbound/remote/${aggregateRoot.enName.lowercase()}biz".replace(
                    "/",
                    "."
                )
            //创建pom.xml
            generateRemotePomXml(aggregateRoot,path.appendNextPath(aggregateRoot.moduleName.lowercase()+"bc").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound-remote"))
            //创建local文件夹
            generateRemoteFolder(remoteRootPath)
            //创建包路径
            generatePackageInfo(remoteRootPath.substringBeforeLast("/"), remoteJavaPath.substringBeforeLast("."))

        }

        private fun generateRemoteFolder(path: String) {
            FileUtil.mkdir(path)
        }


        private fun generateRemotePomXml(
            aggregateRoot: AggregateRootEntity,
            path: String
        ) {
            val xmlContent =
"""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>${aggregateRoot.comPrefix}</groupId>
        <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound</artifactId>
        <version>${aggregateRoot.version}</version>
    </parent>

    <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound-remote</artifactId>

    <dependencies>
        <dependency>
            <groupId>${aggregateRoot.comPrefix}</groupId>
            <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound-local</artifactId>
            <version>${'$'}{zz.project.version}</version>
        </dependency>
        <dependency>
           <groupId>${aggregateRoot.comPrefix}</groupId>
           <artifactId>${aggregateRoot.moduleName.lowercase()}bc-client</artifactId>
           <version>${'$'}{zz.project.version}</version>
        </dependency>
        <dependency>
           <groupId>com.zz</groupId>
           <artifactId>zz-core-secure</artifactId>
       </dependency>
    </dependencies>

</project>
                """
            createFile(path.appendNextPath("pom.xml"), xmlContent)
        }

    }

}